using System;
using System.Drawing;

namespace Core
{
    public static class BoardExtensions
    {
        public static Board FillEmptySpaces(this Board board)
        {
            var rand = new Random();
            for (int i = 0; i < board.AllTiles.Count; i++)
            {
                var tile = board.AllTiles[i];
                if (tile == null)
                {
                    board.AllTiles[i] = new Tile(((char)rand.Next(65, 90)).ToString(), 1);
                }
            }
            return board;
        }

        public static Bitmap GetBitmap(this Board board, int width, int height)
        {
            var bitmap = new Bitmap(width, height);
            var graphics = Graphics.FromImage(bitmap);
            graphics.Clear(SystemColors.Control);
            const int spacing = 50;
            const int half_spacing = spacing / 2;
            var column = 0;
            var row = 0;
            Pen shade = new Pen(Color.Goldenrod, 2);
            var format = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };
            var scoreformat = new StringFormat
            {
                Alignment = StringAlignment.Far,
                LineAlignment = StringAlignment.Far
            };

            for (int i = 0; i < board.AllTiles.Count; i++)
            {
                column++;
                var xoffset = 0;
                if (row % 2 == 0)
                {
                    //long row
                    if (column > board.Width)
                    {
                        row++;
                        column = 1;
                        xoffset = half_spacing;
                    }
                }
                else
                {
                    //short row
                    xoffset = half_spacing;
                    if (column > (board.Width - 1))
                    {
                        row++;
                        column = 1;
                        xoffset = 0;
                    }
                }
                xoffset -= spacing;
                var rect = new RectangleF(xoffset + (column*spacing), row*spacing, spacing, spacing);
                var innerrect = new Rectangle((int) (rect.X + 2), (int) (rect.Y + 2), (int) (rect.Width - 3), (int) (rect.Height - 3));
                graphics.FillRectangle(Brushes.PaleGoldenrod, rect);
                graphics.DrawRectangle(shade, innerrect);
                graphics.DrawString(board.AllTiles[i].Character, new Font("Verdana", half_spacing), Brushes.Black, rect, format);
                graphics.DrawString(board.AllTiles[i].Score.ToString(), new Font("Courier New", half_spacing / 3), Brushes.Black, innerrect, scoreformat);
                graphics.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width,rect.Height);
            }
            graphics.Dispose();
            return bitmap;
        }
    }
}